import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { NetService } from '../../core/services/net-service/net.service';
import { MessageService } from '../../core/services/message-service/message.service';
import { WebApi } from '../../core/config/webapi_mapping';
import {ActivatedRoute, Params, Router} from '@angular/router'

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {

  validateForm!: FormGroup;

  account: string;

  password: string;

  constructor(private fb: FormBuilder, private netService: NetService, private router:Router, private messageService: MessageService, private activeRoute: ActivatedRoute) {}

  login(): void {

    let data = {
      account: this.account,
      password: this.password
    }
    
    this.netService.postForm(WebApi.admin.login, data).subscribe(
      (result) => {
        if (result.state === '0') {
          this.messageService.setState('admin', result.data)
          this.router.navigate(['/frame'])
        }
      }
    );
  }

  ngOnInit(): void {
    this.validateForm = this.fb.group({
      userName: [null, [Validators.required]],
      password: [null, [Validators.required]],
      remember: [true]
    });

    this.account = 'admin'
    this.password = '123456'

    this.activeRoute.queryParams.subscribe((params: Params) => {
      this.messageService.setState('code',params['code'])
    });
  }
}